package com.jianyun.jyzs.activity.newsign;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hannesdorfmann.mosby3.mvp.MvpActivity;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.adapter.NewSign2RecordAdapter;
import com.jianyun.jyzs.adapter.NewSignRecordAdapter;
import com.jianyun.jyzs.bean.NewSign2Bean;
import com.jianyun.jyzs.bean.NewSignRecodeBean;
import com.jianyun.jyzs.common.CustomLinearLayoutManager;
import com.jianyun.jyzs.presenter.NewSignRePresenter;
import com.jianyun.jyzs.utils.DateUtils;
import com.jianyun.jyzs.utils.FullDialogLoading;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.view.iview.INewSignRecordView;
import com.jianyun.jyzs.widget.MonthWeekMaterialCalendarView;
import com.jrmf360.rylib.common.util.ToastUtil;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yangshijun on 2018/6/11.
 * 签到记录
 */

public class NewSignReCordActivity extends MvpActivity<INewSignRecordView, NewSignRePresenter> implements INewSignRecordView {

    @BindView(R.id.calendarView_month_mode)
    MaterialCalendarView calendarViewMonthMode;
    @BindView(R.id.calendarView_week_mode)
    MaterialCalendarView calendarViewWeekMode;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.weekview_top)
    LinearLayout calendarView_onlyhas_weekName;
    @BindView(R.id.slidelayout)
    MonthWeekMaterialCalendarView monthWeekMaterialCalendarView;
    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.top_rl)
    RelativeLayout topRl;


    private CalendarDay selectedDate;
    private NewSignRecordAdapter adapter;
    private NewSign2RecordAdapter adapter2;
    private String enterpriseCode;
    private String userId;
    private FullDialogLoading dialog;
    private String sign;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_sign_record);
        ButterKnife.bind(this);
        sign = getIntent().getStringExtra("sign");
        initData();
        initCalendView();
        initRecyclerView();

    }

    private void initData() {
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        userId = LoginCache.getInstance().getUserInfo().getUserId();
        selectedDate = CalendarDay.today();
        String yearMonth = DateUtils.getYearMonth(selectedDate);
        title.setText(yearMonth);
        String formatDate = DateUtils.getFormatDate(selectedDate);
        dialog = FullDialogLoading.createDialog(this);
        dialog.setMessage("loading...");
        getPresenter().getSignData(enterpriseCode, userId, formatDate);
    }

    @NonNull
    @Override
    public NewSignRePresenter createPresenter() {
        return new NewSignRePresenter();
    }

    private void initCalendView() {
        monthWeekMaterialCalendarView.setCurrentDate(selectedDate);
        monthWeekMaterialCalendarView.setSelectedDate(selectedDate);
        //默认是周模式
        monthWeekMaterialCalendarView.state().edit().setSlideModeChangeListener(new MonthWeekMaterialCalendarView.SlideModeChangeListener() {
            @Override
            public void modeChange(MonthWeekMaterialCalendarView.Mode mode) {
                if (mode.equals(MonthWeekMaterialCalendarView.Mode.MONTH)) {

                }
                if (mode.equals(MonthWeekMaterialCalendarView.Mode.WEEK)) {

                }
            }
        }).setSlideDateSelectedlistener(new MonthWeekMaterialCalendarView.SlideDateSelectedlistener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
                selectedDate = date;
                String formatDate = DateUtils.getFormatDate(date);
                getPresenter().getSignData(enterpriseCode, userId, formatDate);
                //Log.i("test", "选择日期：" + formatDate);
            }
        }).setSlideOnMonthChangedListener(new MonthWeekMaterialCalendarView.SlideOnMonthChangedListener() {
            @Override
            public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
                String yearMonth = DateUtils.getYearMonth(date);
                title.setText(yearMonth);
            }
        }).commit();

        AddDecorator();
    }

    private void initRecyclerView() {
        List<NewSign2Bean.SignDetail> list = new ArrayList<>();

        recyclerView.setLayoutManager(new CustomLinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false));
        recyclerView.setHasFixedSize(true);
        if (null != sign) {
            adapter2 = new NewSign2RecordAdapter(this, list);
            recyclerView.setAdapter(adapter2);
        } else {
            adapter = new NewSignRecordAdapter(this, list);
            recyclerView.setAdapter(adapter);
            adapter.setOnItemClickListener(new NewSignRecordAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position, View view, NewSignRecodeBean.SignDetail listdata) {
                    //跳转等
                    Log.i("tset", "补卡：" + position);
                }
            });
        }

    }


    private void AddDecorator() {
        //150天
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.MONTH, -2);
//        ArrayList<CalendarDay> dates = new ArrayList<>();
//        for (int i = 0; i < 30; i++) {
//            CalendarDay day = CalendarDay.from(calendar);
//            dates.add(day);
//            calendar.add(Calendar.DATE, 5);
//        }
//        //增加有红点标志
//        monthWeekMaterialCalendarView.addDecorator(new EventDecorator(Color.RED, dates));
    }

    @OnClick(R.id.topBack)
    public void onViewClicked() {
        finish();

    }

    @Override
    public void showLoading() {
        dialog.show();
    }

    @Override
    public void hintLoading() {
        dialog.dismiss();
    }

    @Override
    public void onSuccess(List<NewSign2Bean.SignDetail> signDetail) {
        if (signDetail != null) {
            if (null!=sign){
                adapter2.setDataList(signDetail);

            }else {
                adapter.setDataList(signDetail);

            }
        }
    }

    @Override
    public void onFailed(String msg) {
        ToastUtil.showToast(this, msg);
    }
}
